package com.example.deliveryapplication.wiget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import com.example.deliveryapplication.*
import com.example.deliveryapplication.entity.BearingType
import com.example.deliveryapplication.entity.SelectString
import kotlinx.android.synthetic.main.add_bear_model_dialog.*


class AddBearModelDialog(context: Context): Dialog(context) {
//    private var companyNameTV:TextView?=null
//    private var mConfirmBTN:Button?=null
    private var listener:CallBack?=null
    private var typeList:List<SelectString>?=null
    private var data:SelectString?=null
    private var isEdit:Boolean=false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_bear_model_dialog)
        val lp = window?.attributes
        // 设置窗口宽度为屏幕宽度的80%
        val displayMetrics = context.resources.displayMetrics
        lp?.width = (displayMetrics.widthPixels * 0.8).toInt()
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp?.gravity = Gravity.CENTER
        window?.attributes = lp
        mConfirmBTN.setOnClickListener {
            val name=companyNameTV.text.toString()
            if(name.isNullOrEmpty()){
               context.showToast("请先输入公司名称")
            }else{
                if(bearTypeSP.selectedItem==null){

                }else{
                    val bean=bearTypeSP.selectedItem as SelectString
                    Log.d("测试",bean.toString())
                    listener?.onConfirmClick(name,bean)
                    dismiss()
                }


            }
        }
        typeList?.let {
            val adapter = StringListSpinnerAdapter(context, it)
            bearTypeSP.adapter = adapter
        }
        data?.let {
            companyNameTV.setText(it.param["name"].handlerNull())
        }
        val index = typeList?.indexOfFirst { t ->
            data?.let { d ->
                t.code == d.param["typeId"]
            } ?: false
        }
        if (index != -1) {
            // 找到了满足条件的元素
            // 索引位置为 index
            index?.let { bearTypeSP.setSelection(it) }
        } else {
            // 没有找到满足条件的元素
        }
        if(isEdit){
            mTitleTV.text="修改型号"
        }
    }
    fun setTypeList(list:List<SelectString>){
        this.typeList=list
    }
    fun setListener(listener:CallBack){
        this.listener=listener
    }
    fun setData(data:SelectString){
        this.data=data
    }
    fun setEdit(isEdit:Boolean){
        this.isEdit=isEdit
    }
    interface CallBack{
        fun onConfirmClick(name:String,data:SelectString)
    }
}
